//------------------------------------------------------------------------------
// Fichiers d'ent�te utilis�s
//------------------------------------------------------------------------------
#include "ConvertRGBToGrayOperator.hpp"
#include "Region.hpp"
#include <math.h>
#include "Tools/LoggerFile.hpp"
#include "iostream"
//------------------------------------------------------------------------------
// Définition de constantes
//------------------------------------------------------------------------------

namespace ImageAnalysis
{

	//----------------------------------------------------------------------
	// Constructeur
	//----------------------------------------------------------------------
	ConvertRGBToGrayOperator::ConvertRGBToGrayOperator( RGBImage* color, GrayImage* gray ):
	colorImage(color),
	grayImage(gray)	
	{	
	
	}

	//----------------------------------------------------------------------
	// Destructeur
	//----------------------------------------------------------------------
	ConvertRGBToGrayOperator::~ConvertRGBToGrayOperator( )
	{
	
	}
	
	//----------------------------------------------------------------------
	// 
	//----------------------------------------------------------------------
	void ConvertRGBToGrayOperator::Process( )
	{	
		int imageSize  = colorImage->GetWidth()*colorImage->GetHeight();
		unsigned char* pixels;
		unsigned int average;
		unsigned char thresholdAverage;
		for( int index=0; index < imageSize; ++index )
		{
			pixels = colorImage->GetPixel( index );
			average = (pixels[0]+pixels[1]+pixels[2])/3;
		    	thresholdAverage = average<=255?average:255;
			grayImage->SetPixel(index ,&thresholdAverage);			
		}	
	}
}

